<template>
  <div>
    <div class="card" style="padding: 15px">
      您好，{{ user?.name }}！欢迎使用本系统
    </div>

    <div style="display: flex; margin: 10px 0">
      <div style="width: 50%;margin-right: 10px" class="card">
        <div style="margin-bottom: 30px; font-size: 20px; font-weight: bold">公告列表</div>
        <div >
          <el-timeline  reverse slot="reference">
            <el-timeline-item v-for="item in notices" :key="item.id" :timestamp="item.time">
              <el-popover
                  placement="right"
                  width="200"
                  trigger="hover"
                  :content="item.content">
                <span slot="reference">{{ item.title }}</span>
              </el-popover>
            </el-timeline-item>
          </el-timeline>
        </div>
      </div>
      <div class="card" style="width:50%" v-if="user.role === 'ADMIN'">
        <div style="margin-bottom: 10px; font-size: 20px; font-weight: bold">商家销售额排行</div>
        <el-scrollbar style="height: 100%">
          <ul v-if="user.role==='ADMIN'">
            <li v-for="i in businessData" class="infinite-list-item">{{ i.name }}：{{i.total}}元</li>
          </ul>
        </el-scrollbar>
      </div>
      <el-col  :span="12" v-if="user.role === 'BUSINESS'">
        <el-card>
          <div style="width: 100%; height: 250px" id="bar"></div>
        </el-card>
      </el-col>
    </div>




    <el-row :gutter="10" v-if="user.role === 'BUSINESS'">
      <el-col :span="12">
        <el-card>
          <div style="width: 100%; height: 250px" id="line"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
      <el-card>
        <div style="width: 100%; height: 250px" id="pie"></div>
      </el-card>
    </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'
const option = {
  title: {
    text: '每日趋势图',
    left: 'center'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    left: 'left'
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '金额',
      data: [],
      type: 'line',
      smooth: true
    },
  ]
}

const option1 = {
  title: {
    text: '商品销售图',
    left: 'center'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    left: 'left'
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '金额',
      data: [],
      type: 'bar',
      smooth: true
    }
  ]
}

const option2 = {
  title: {
    text: '分类占比图',
    subtext: '比例图',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: '金额',
      type: 'pie',
      center: ['50%', '60%'],
      radius: '50%',
      data: [],
      label: {
        show: true,
        formatter(param) {
          return param.name + ' (' + param.percent + '%)';
        }
      },
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
}
export default {
  name: 'Home',
  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      notices: [],
      businessData:[]
    }
  },
  mounted() {  // 等待页面的元素全部加载完成之后再初始化

    // 折线图
    let linetDom = document.getElementById('line');
    let lineChart = echarts.init(linetDom);


    // 柱状图
    let barDom = document.getElementById('bar');
    let barChart = echarts.init(barDom);
    barChart.setOption(option1)

    // 饼图
    let pieDom = document.getElementById('pie');
    let pieChart = echarts.init(pieDom);
    pieChart.setOption(option2)

    this.$request.get('/charts',{businessId:this.user.id}).then(res => {
      // 折线
      option.xAxis.data = res.data?.line?.map(v => v.date) || []
      option.series[0].data = res.data?.line?.map(v => v.value) || []
      lineChart.setOption(option)

      // 柱状图
      option1.xAxis.data = res.data?.bar?.map(v => v.name) || []
      option1.series[0].data = res.data?.bar?.map(v => v.value) || []
      barChart.setOption(option1)

      // 饼图
      option2.series[0].data = res.data?.pie || []
      pieChart.setOption(option2)
    })
  },

  created() {
    this.$request.get('/notice/selectAll').then(res => {
      this.notices = res.data || []
    })
    this.$request.get('/data').then(res =>{
      this.businessData = res.data
    })
  },
  methods:{
    load () {
      this.count += 2
    }
  }
}
</script>
<style>
.el-scrollbar__wrap {
  overflow-x: hidden !important;
}
.infinite-list-item{
  background-color:#e8f3fe;
  margin: 5px;
  height: 30px;
  line-height: 30px;
  padding-left: 10px;
}
</style>